#include "menu.h"
#include "definitions.h"

void printMenu(Menu* menu, int posX, int posY)
{	
	unsigned int i;
	
	if((*menu).function != NULL)
		(*menu).function();
	
	drawStringAt((char*)(*menu).title, posX, posY, YELLOW, BLACK,1, video, table);
	
	for (i = 0; i < (int)(*menu).options_size; ++i)
	{
		if((int)(*menu).selected_option == i)
			drawStringAt((char*)(*menu).options[i].text, posX, posY + i*20 + 40, BLACK, WHITE,1, video, table);
		else
			drawStringAt((char*)(*menu).options[i].text, posX, posY + i*20 + 40, WHITE, BLACK,1, video, table);
			
		if((*menu).options[i].value_function != NULL)
			(*menu).options[i].value_function(posX + 100, posY + i*20 + 40);
	}
}



void startMenu(Menu* menu, int posX, int posY)
{
	int sair = 0;
	int scancode;
	
	clear_screen(BLACK,video);
	//printf("%dx%d",H,V);
	do
	{
		printMenu(menu,posX,posY);
	
		if(!queueEmpty(&keys_queue))
		{
			scancode = queueGet(&keys_queue);
				
			if(scancode == key_up || scancode == key_down)				
			{	
				if(scancode == key_up)
				{
					if((*menu).selected_option != 0)
						(*menu).selected_option--;
					else
						(*menu).selected_option = (*menu).options_size - 1;
				}
				
				if(scancode == key_down)
				{
					if((*menu).selected_option != (*menu).options_size - 1)
						(*menu).selected_option++;
					else
						(*menu).selected_option = 0;
				}
				
			}
			
			if(scancode == key_action)
			{
				if((*menu).options[(*menu).selected_option].process_function != NULL)
				{
					sair = (*menu).options[(*menu).selected_option].process_function();
					clear_screen(BLACK,video);
				}
				else
					sair = 1;
					
			}
			
			if(scancode == ESC)
				sair = 1;
		}
		
	}while(sair == 0);

}
